package com.sky.migrate.common;

import com.zaxxer.hikari.HikariConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;

public class DataSourceConfigUtil {

    public static HikariConfig setDataSourceEnvConfig(String prefix1, Environment env) {

        HikariConfig config = new HikariConfig();
        String driver = env.getProperty(prefix1 + "driver-class-name");
        String dataSourceUrl = env.getProperty(prefix1 + "jdbc-url");
        String user = env.getProperty(prefix1 + "username");
        String password = env.getProperty(prefix1 + "password");

        if (StringUtils.isNotBlank(dataSourceUrl)) {
            config.setJdbcUrl(dataSourceUrl);
        }
        if (StringUtils.isNotBlank(user)) {
            config.setUsername(user);
        }
        if (StringUtils.isNotBlank(password)) {
            config.setPassword(password);
        }
        if (StringUtils.isNotBlank(driver)) {
            config.setDriverClassName(driver);
        }
        return config;
    }

}
